# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

_luaversions="5.1 5.2 5.3 5.4"

pkgname=lua-lzlib
_name=lzlib
pkgver=0.4.3
pkgrel=2
pkgdesc="Lua bindings to the ZLib compression library"
url="https://github.com/LuaDist/lzlib"
arch="all"
license="MIT"
makedepends="zlib-dev"
for _i in $_luaversions; do
	makedepends="$makedepends lua$_i-dev"
	subpackages="$subpackages lua$_i-lzlib:_zlib_${_i/./_}"
done
source="$pkgname-$pkgver.tar.gz::https://github.com/LuaDist/$_name/archive/$pkgver.tar.gz"

builddir="$srcdir"/$_name-$pkgver
options="!check"

prepare() {
	default_prepare

	for _i in $_luaversions; do
		cp -a "$builddir" "$srcdir"/build-$_i
	done
}

build() {
	for _i in $_luaversions; do
		cd "$srcdir"/build-$_i
		msg "build for Lua $_i"
		make \
			CFLAGS="$CFLAGS $(pkg-config --cflags lua$_i)" \
			LDFLAGS="$LDFLAGS $(pkg-config --libs lua$_i)"
	done
}

package() {
	mkdir -p "$pkgdir"
}

_split_zlib() {
	local _ver=$1
	pkgdesc="$pkgdesc for Lua $_ver"
	install_if="lua$_ver $pkgname=$pkgver-r$pkgrel"
	depends=""

	cd "$srcdir"/build-$_ver
	install -Dm755 zlib.so "$subpkgdir"/usr/lib/lua/$_ver/zlib.so
	install -Dm644 gzip.lua "$subpkgdir"/usr/share/lua/$_ver/gzip.lua
}

for _i in $_luaversions; do
	eval "_zlib_${_i/./_}() { _split_zlib $_i; }"
done

sha512sums="7f90533091ed4917019a26c1a944101dffbaaeac8c6608d9a9e078e624f8598f46e8fee939680dcea95317d752ca6bb85b21fb611f003ddbae362fa36e05e9c3  lua-lzlib-0.4.3.tar.gz"
